package com.synjones.handsetS8L;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import com.mx.finger.alg.MxIDFingerAlg;
import com.mx.finger.api.msc.MxIdMscFingerApiFactory;
import com.mx.finger.api.msc.MxMscBigFingerApi;
import com.mx.finger.common.MxImage;
import com.mx.finger.common.Result;
import com.mx.finger.utils.RawBitmapUtils;
import com.synjones.cardutil.util;

import java.util.HashMap;
import java.util.Iterator;

public class MxFingerModule implements FingerprintReaderInterface {

    private MxMscBigFingerApi mxMscBigFingerApi;
    private MxIDFingerAlg mxFingerAlg;
    Context context;
    byte fea[];
    public MxFingerModule(Context context){
        MxIdMscFingerApiFactory fingerFactory = new MxIdMscFingerApiFactory(context);
        mxMscBigFingerApi = fingerFactory.getApi();
        mxFingerAlg = fingerFactory.getAlg();
        this.context=context;
    }

    @Override
    public void powerOn() {
        PowerUtil.powerFinger ("1");
        PowerUtil.powerUsb("1");
        util.mSleep(1000);
        requestPermission();
    }

    @Override
    public void powerOff() {
        PowerUtil.powerFinger ("0");
        PowerUtil.powerUsb("0");
    }
    public void requestPermission() {
        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        UsbDevice usbDevice = enumerateDevice(usbManager);
        if(usbDevice==null) return;
        if (usbManager.hasPermission(usbDevice)) {

        } else {
            PendingIntent comm = PendingIntent.getBroadcast(this.context, 0, new Intent("com.IDWORLD.USB_PERMISSION"), 0);
            usbManager.requestPermission(usbDevice, comm);
        }
    }


    private UsbDevice enumerateDevice(UsbManager usbManager) {
        if(usbManager != null) {
            HashMap deviceList = usbManager.getDeviceList();
            if(!deviceList.isEmpty()) {
                StringBuffer sb = new StringBuffer();
                Iterator var4 = deviceList.values().iterator();

                while(true) {
                    while(var4.hasNext()) {
                        UsbDevice device = (UsbDevice)var4.next();
                        if(device.getVendorId() == 0x0401 && device.getProductId() == 0x542B) {
                            return device;
                        }
                    }

                    return null;
                }
            }
        }
        return null;
    }

    @Override
    public int open() {

        Result<String> deviceInfo = mxMscBigFingerApi.getDeviceInfo();
        if(deviceInfo.isSuccess())
            return 0;

        return -1;
    }

    @Override
    public int close() {
        return 0;
    }

    @Override
    public int cleanFpFeature() {
        fea=null;
        return 0;
    }

    @Override
    public FpFeature getFpFeature(boolean showFpImage, long timeout) {
        Result<MxImage> result = mxMscBigFingerApi.getFingerImageBig(timeout);
        if (!result.isSuccess()) {
            return null;
        }
        //step 1 extract finger feature
        MxImage image = result.data;
        assert image != null;
        byte[] featureBufferEnroll = mxFingerAlg.extractFeature(image.data, image.width, image.height);
        if (featureBufferEnroll == null) {
            return null;
        }

        if(showFpImage)
            return new FpFeature(getFingerBitmap(image),1,featureBufferEnroll,"");
        else
            return new FpFeature(null,1,featureBufferEnroll,"");
    }

    private Bitmap getFingerBitmap(MxImage image){

        return RawBitmapUtils.raw2Bimap(image.data, image.width, image.height);
    }


    @Override
    public int setFpFeature(int toWhere, byte[] data) {
        if(data==null||data.length<=0) return -1;
        fea=new byte[data.length];
        System.arraycopy(data,0,fea,0,data.length);
        return 0;
    }

    @Override
    public FpVerifyResult verifyFp(int sThreshold, long timeout) {

        if(fea==null){
            return null;
        }

        Result<MxImage> result = mxMscBigFingerApi.getFingerImageBig(timeout);
        if (!result.isSuccess()) {
            return null;
        }
        MxImage image = result.data;
        //step 1 get finger feature
        assert image != null;
        byte[] featureBufferMatch = mxFingerAlg.extractFeature(image.data, image.width, image.height);
        if (featureBufferMatch == null) {
            return null;
        }
        //step 2 match finger feature
        int match = mxFingerAlg.match(fea, featureBufferMatch, 3);
        if (match == 0) {
            return new FpVerifyResult(true, 100, 0);
        } else {
            return new FpVerifyResult(false, 0, 0);
        }
    }

    @Override
    public void cancel() {

    }

    @Override
    public int calibration() {
        return 0;
    }
}
